package com.sunyard.utils;

import lombok.extern.slf4j.Slf4j;
import sun.rmi.runtime.Log;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author 130459
 * @ClassName JDBCSimpleUtil
 * @PackageName com.ecc.liana.services.http.util
 * @Description 用于科技渠道较底层开发的java方法库
 * @date 2016-3-22 下午04:18:51
 * @Version: V1.0
 */
@Slf4j
public class ExtendsSimpleUtil {

    /**
     * @function:获取服务器Ip
     * @date 20150603
     * @author gj@nbcb
     */
    public static String getServerIp() {
        //定义变量
        String serverIp = "";

        InputStreamReader isr = null;
        BufferedReader br = null;
        Process jprocess = null;


        //aix或者linux
//        if (SystemIdentifier.isAIX()) {
//            //定义命令集和
//            String[] cmds = {"/bin/sh", "-c", "cat /etc/hosts|grep ipayIP"};
//
//            try {
//                //执行命令
//                jprocess = Runtime.getRuntime().exec(cmds);
//
//                //获取命令输入流
//                isr = new InputStreamReader(jprocess.getInputStream());
//                //读取流
//                br = new BufferedReader(isr);
//                String line = null;
//
//                //读取第一行
//                if ((line = br.readLine()) != null) {
//                    line = line.trim();
//                    serverIp = line.replaceAll("ipayIP", "").trim();
//                }
//
//            } catch (Exception e) {
//                e.getMessage();
//            } finally {
//                if (br != null) {
//                    try {
//                        br.close();
//                    } catch (IOException e) {
//                        e.getMessage();
//                    }
//                }
//                if (isr != null) {
//                    try {
//                        isr.close();
//                    } catch (IOException e) {
//                        e.getMessage();
//                    }
//                }
//
//                if (jprocess != null) {
//                    try {
//                        jprocess.destroy();
//                    } catch (Exception e) {
//                        e.getMessage();
//                    }
//                }
//            }
//        } else {
//            //定义命令语句
//        	String cmd = "cmd /c ipconfig";
//
//            try {
//                //执行命令
//                jprocess = Runtime.getRuntime().exec(cmd);
//
//                //获取命令输入流
//                isr = new InputStreamReader(jprocess.getInputStream());
//                //读取流
//                br = new BufferedReader(isr);
//                String line = null;
//
//                while ((line = br.readLine()) != null) {
//                    line = line.trim();
//
//                    if (!"".equals(line)
//                            && (line.startsWith("IP Address") || line
//                            .startsWith("IPv4"))) {
//                        serverIp = line.split(":")[1].trim();
//                    }
//                }
//
//            } catch (Exception e) {
//                e.getMessage();
//            } finally {
//                if (br != null) {
//                    try {
//                        br.close();
//                    } catch (IOException e) {
//                        e.getMessage();
//                    }
//                }
//
//                if (isr != null) {
//                    try {
//                        isr.close();
//                    } catch (IOException e) {
//                        e.getMessage();
//                    }
//                }
//                if (jprocess != null) {
//                    try {
//                        jprocess.destroy();
//                    } catch (Exception e) {
//                        e.getMessage();
//                    }
//                }
//            }
//        }
        serverIp = IpUtils.getLocalIp();
        log.info("serverIp=" + serverIp);
        if (serverIp.contains("addr:")) {
            log.info(serverIp);
            serverIp = serverIp.replace("addr:", "");
            log.info(serverIp);
        }
        log.info("serverIp:=" + serverIp);
        return serverIp;
    }


    /**
     * 获取服务器ip
     *
     * @return
     */
    public static String getServerIp02() {
        //定义变量
        String serverIp = "";

        InputStreamReader isr = null;
        BufferedReader br = null;
        Process jprocess = null;

        //aix或者linux
        if (SystemIdentifier.isAIX()) {
            //定义命令集和
            String[] cmds = {"/bin/sh", "-c", "ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | awk '{print $2}'"};

            try {
                //执行命令
                jprocess = Runtime.getRuntime().exec(cmds);

                //获取命令输入流
                isr = new InputStreamReader(jprocess.getInputStream());
                //读取流
                br = new BufferedReader(isr);
                String line = null;
                //log.info("line data="+line);
                while ((line = br.readLine()) != null) {
                    line = line.trim();

                    if (!"".equals(line)) {
                        serverIp = line;
                    }
                }

            } catch (Exception e) {
                e.getMessage();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.getMessage();
                    }
                }

                if (isr != null) {
                    try {
                        isr.close();
                    } catch (IOException e) {
                        e.getMessage();
                    }
                }
                if (jprocess != null) {
                    try {
                        jprocess.destroy();
                    } catch (Exception e) {
                        e.getMessage();
                    }
                }
            }
        } else {
            //定义命令语句

            String cmd = "cmd /c ipconfig";

            try {
                //执行命令
                jprocess = Runtime.getRuntime().exec(cmd);

                //获取命令输入流
                isr = new InputStreamReader(jprocess.getInputStream());
                //读取流
                br = new BufferedReader(isr);
                String line = null;

                while ((line = br.readLine()) != null) {
                    line = line.trim();

                    if (!"".equals(line)
                            && (line.startsWith("IP Address") || line
                            .startsWith("IPv4"))) {
                        serverIp = line.split(":")[1].trim();
                    }
                }

            } catch (Exception e) {
                e.getMessage();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.getMessage();
                    }
                }

                if (isr != null) {
                    try {
                        isr.close();
                    } catch (IOException e) {
                        e.getMessage();
                    }
                }
                if (jprocess != null) {
                    try {
                        jprocess.destroy();
                    } catch (Exception e) {
                        e.getMessage();
                    }
                }
            }
        }

        return serverIp;
    }


    /**
     * @function:获取服务器名称
     * @date 20150603
     * @author gj@nbcb
     */
    public static String getServerName() {
        //定义变量
        String serverName = "";

        //aix系统或者linux系统
        if (SystemIdentifier.isAIX()) {
            try {
                Class serverNameCalss = Class
                        .forName("com.ibm.websphere.runtime.ServerName");
                java.lang.reflect.Method getName = serverNameCalss.getMethod(
                        "getDisplayName", null);
                serverName = (String) getName.invoke(serverNameCalss, null);
            } catch (Exception e) {
                serverName = "error";
            }
        }
        //其他直接写死
        else {
            serverName = "other";
        }
        return serverName;
    }

    /**
     * @Description 获取客户端ip地址
     * @Param [request]
     * @Return java.lang.String
     */
    public static String getClientIp(HttpServletRequest request) {
        String ipAddress = request.getHeader("x-forwarded-for");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if ("127.0.0.1".equals(ipAddress) || "0:0:0:0:0:0:0:1".equals(ipAddress)) {
                //根据网卡取本机配置的ip
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                    ipAddress = inet.getHostAddress();
                } catch (UnknownHostException e) {
                    throw new RuntimeException(e);
                } catch (NullPointerException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        //对于通过多个代理的情况, 第一个IP为客户端真实IP,多个IP按照","分割
        if (ipAddress != null && ipAddress.length() > 15) {
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }
}
